<?php 
/*
Copyright 2012 Bart Erven

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

session_start();
require_once("_keychain.php");
if ($_SESSION['access'] == false)
{
	if ($_POST['username'] == $_config['admin']['username'] && $_POST['password'] == $_config['admin']['password'])
	{
		$_SESSION['access'] = true;
	}
	else
	{
		die ('Wrong username/password. <a href="./">back</a>');
	}
}

require_once ('dbase.php');
?>
<!DOCTYPE html>
<html>
<head>
	<title>Twotbot.</title>
	<link rel="stylesheet" href="css/bootstrap.min.css">
	<style type="text/css">
		
		textarea
		{
			width: 400px;
			height: 100px;
		}
		
		body
		{
			margin: 20px;
		}
		
	</style>
</head>
<body>
<?php

if ($_POST['tweet'])
{
	mysql_query("INSERT INTO tweets (tweet) VALUES ('{$_POST['tweet']}')");
	
	echo "Added new tweet!<br /><br />";
}

$result = mysql_query("SELECT id FROM tweets WHERE tweeted = 0");
$count = mysql_num_rows($result);
if ($count > 0)
{
	echo $count ." tweets before recycling starts.<br /><br />";
}


$query = urlencode(file_get_contents("_query.txt"));
$json = file_get_contents("http://search.twitter.com/search.json?q=$query");

$data = json_decode($json);

//print_r($data);

foreach ($data->results as &$tweet)
{

	echo '<form method="post" action="?">';
	echo "@" . $tweet->from_user;
	
	echo "<br />";
	echo '<textarea name="tweet">'.$tweet->text.'</textarea><br />';
	echo '<input class="btn btn-primary" type="submit" value="Add" />';
	echo '</form><hr />';
	
}
?>

</body>